<?php
if (!defined("__WD__")) exit("Wrong Connect");

class Kong extends __Kong{
	
	function __construct() {

		parent::__construct();

		$this->load->model("admin/admin", $this->admin);

		$this->load->module( "mysql" , $this->db );
		$this->load->module( "input" , $this->input );
		$this->load->module( "ajax" , $this->ajax );
		
		$this->data->title = $this->data->kong->name." - ".$this->data->title;


	}
	function doit($args) {
		$stat1 = (isset($args[0]))?$args[0]:"article";
		$stat2 = (isset($args[1]))?$args[1]:"list";
		$stat3 = (isset($args[2]))?$args[2]:"default";
		$stat4 = (isset($args[3]))?$args[3]:"default";
		
		// Action!
		if ($this->input->post_exists("action")) {
			// kong Class.		
			$this->load->kong("proc");
			$this->proc = new Kong_Proc();
			$this->proc->doit();
		}
				
		$this->data->status = $stat1; // 기본 메뉴.
		
		switch ( $stat1 ) {
			case "article" :
				switch ( $stat2 ) {
					case "list" :
						$this->viewArticleList();
						break;
					default :
						$this->viewArticleModify( $stat2 );
						break;
				}
				break;
			case "comment" :
				switch ( $stat2 ) {
					case "list" :
						$this->viewCommentList();
						break;
					default :
						$this->viewCommentModify( $stat2 );
						break;
				}
				break;
			case "board" :
				switch ( $stat2 ) {
					case "list" :
						$this->viewBoardList();
						break;
					case "new" :
						$this->viewBoardNew();
						break;
					default : // modify
						switch( $stat3 ) {
							case "default" :
								$this->viewBoardModify( $stat2 );
								break;							
							case "category" :
								$this->viewBoardCategory( $stat2 );
								break;							
							case "grant" :
								$this->viewBoardGrant( $stat2 );
								break;							
						}
						break;
				}
				break;
			case "configure" :
				switch ( $stat2 ) {
					case "list" :
						break;
					case "new" :
						break;
					case "modify" :
						break;
				}
				break;
			case "ajax" :
				
				break;
		}
		
	}

	function viewArticleList() {

		$board		= $this->db->fetch_assoc("select `id`,`name` from `wd_board`");
		$category	= $this->db->fetch_assoc("select `idx`,`name` from `wd_category`");

		foreach( $board as $v )  $this->data->board[ $v['id'] ] = $v['name'];
		foreach( $category as $v )  $this->data->category[ $v['idx'] ] = $v['name'];
		
		unset($board, $category);

		$page = $this->input->get("page", 1);
		$len_line = 20;
		
		$len = $this->db->count("select count(*) from `wd_data`");
		$rows = $this->db->fetch_assoc("select * from `wd_data` order by `idx` desc limit ".($page-1)*$len_line.", {$len_line}");

		$cnt = count($rows);
		for ($i = 0; $i < $cnt; $i++) {
			$rows[$i]['n'] = $len - $i - ($page-1)*$len_line;
			$rows[$i]['options'] = @unserialize($rows[$i]['options']);
			$rows[$i]['comment_num'] = $this->db->count("select count(*) from `wd_comment` where `data_idx`=?", array($rows[$i]['idx']));
		}
		unset($cnt);

		$pagination = $this->admin->makePagination($len, array(
			"page" => $page,
			"len_line" => $len_line
		));

		$this->data->rows = $rows;
		$this->data->pagination = $pagination;
		
		// -- 끝.
		$this->_header();
		$this->load->kong("view/header");
		$this->load->kong("view/article/list");
		$this->load->kong("view/footer");
		$this->_footer();
	
	}

	function viewArticleModify( $idx ) {

		$this->data->result	= $this->db->result( "select * from `wd_data` where `idx`=?", array($idx) );
		$this->data->result['options'] = _unserialize( $this->data->result['options'] );
		$this->data->result['files'] = _unserialize( $this->data->result['files'] );

		// HTML 체크.
		$options = $this->db->result("select `options` from `wd_board` where `id` = ?", array($this->data->result['board_id']) );
		$options = _unserialize($options['options']);
		
		if ( isset($options['html']) && $options['html']) {
			$this->data->admin->redactor = true;
		}
		
		// 댓글가져오기
		$this->data->result['comment'] = $this->db->fetch_assoc("select * from `wd_comment` where `data_idx`=?", array($idx));
		
		$this->js("js/article_new");
		
		$board		= $this->db->fetch_assoc("select `id`,`name` from `wd_board`");
		$category	= $this->db->fetch_assoc("select `idx`,`board_id`,`name` from `wd_category` order by `order` asc");

		foreach( $board as $v )  $this->data->board[ $v['id'] ] = $v['name'];
		foreach( $category as $v )  {
			if ( !isset($cate[ $v['board_id'] ]) ) $cate[ $v['board_id'] ] = array();
			array_push( $cate[ $v['board_id'] ], array($v['idx'], $v['name']) ); 
		}
		
		unset($board, $category);

		$this->data->body .= "<script> var category = ".json_encode($cate).";</script>\r\n";

		$category = $this->db->fetch_assoc("select `idx`,`name` from `wd_category` where `board_id`=?  order by `order` asc", array($this->data->result['board_id']));
		foreach( $category as $v )  $this->data->category[ $v['idx'] ] = $v['name'];

		$this->_header();
		$this->load->kong("view/header");
		$this->load->kong("view/article/modify");
		$this->load->kong("view/footer");
		$this->_footer();
			
			
	}

	function viewCommentList() {

		$board		= $this->db->fetch_assoc("select `id`,`name` from `wd_board`");

		foreach( $board as $v )  $this->data->board[ $v['id'] ] = $v['name'];
		
		unset($board);

		$page = $this->input->get("page", 1);
		$len_line = 20;

		$len = $this->db->count("select count(*) from `wd_comment`");
		$rows = $this->db->fetch_assoc("select	`wd_comment`.`idx` as `idx`, `wd_comment`.`data_idx` as `data_idx`, ifnull(`board_id`, 0) as `board_id`, `subject`, `wd_comment`.`writer` as `writer`, `wd_comment`.`contents` as `contents`, `wd_comment`.`reg_date` as `reg_date`, `wd_comment`.`mod_date` as `mod_date` from `wd_comment` left join `wd_data` on `wd_comment`.`data_idx` = `wd_data`.`idx` order by `wd_comment`.`idx` desc limit ".($page-1)*$len_line.", {$len_line}");

		$cnt = count($rows);
		for ($i = 0; $i < $cnt; $i++) {
			$rows[$i]['n'] = $len - $i - ($page-1)*$len_line;
			$rows[$i]['options'] = @unserialize($rows[$i]['options']);
		}
		unset($cnt);

		$pagination = $this->admin->makePagination($len, array(
			"page" => $page,
			"len_line" => $len_line
		));

		$this->data->rows = $rows;
		$this->data->pagination = $pagination;
		
		// -- 끝.
		$this->_header();
		$this->load->kong("view/header");
		$this->load->kong("view/comment/list");
		$this->load->kong("view/footer");
		$this->_footer();
	
	}

	function viewCommentModify( $idx ) {

		$this->data->result	= $this->db->result( "select * from `wd_comment` where `idx`=?", array($idx) );
		$this->data->result['article'] = $this->db->result("select * from `wd_data` where `idx`=?", array($this->data->result['data_idx']) );
		
		$this->js("js/article_new");

		$board		= $this->db->fetch_assoc("select `id`,`name` from `wd_board`");
		$category	= $this->db->fetch_assoc("select `idx`,`board_id`,`name` from `wd_category` order by `order` asc");

		foreach( $board as $v )  $this->data->board[ $v['id'] ] = $v['name'];
		foreach( $category as $v )  {
			if ( !isset($cate[ $v['board_id'] ]) ) $cate[ $v['board_id'] ] = array();
			array_push( $cate[ $v['board_id'] ], array($v['idx'], $v['name']) ); 
		}
		
		unset($board, $category);

		$this->data->body .= "<script> var category = ".json_encode($cate).";</script>\r\n";

		$category = $this->db->fetch_assoc("select `idx`,`name` from `wd_category` where `board_id`=?  order by `order` asc", array($this->data->result['board_id']));
		foreach( $category as $v )  $this->data->category[ $v['idx'] ] = $v['name'];

		$this->_header();
		$this->load->kong("view/header");
		$this->load->kong("view/comment/modify");
		$this->load->kong("view/footer");
		$this->_footer();
			
			
	}

	function viewBoardList() {

		$page = $this->input->get("page", 1);
		$len_line = 20;
		
		$len = $this->db->count("select count(*) from `wd_board`");
		$rows = $this->db->fetch_assoc("select * from `wd_board` order by `idx` desc limit ".($page-1)*$len_line.", {$len_line}");

		$cnt = count($rows);
		for ($i = 0; $i < $cnt; $i++) {
			$rows[$i]['n'] = $len - $i - ($page-1)*$len_line;
			$rows[$i]['options'] = @unserialize($rows[$i]['options']);
		}
		unset($cnt);

		$pagination = $this->admin->makePagination($len, array(
			"page" => $page,
			"len_line" => $len_line
		));

		$this->data->rows = $rows;
		$this->data->pagination = $pagination;
		
		// -- 끝.
		$this->_header();
		$this->load->kong("view/header");
		$this->load->kong("view/board/list");
		$this->load->kong("view/footer");
		$this->_footer();
		
	}

	function viewBoardNew() {

		$this->_header();
		$this->load->kong("view/header");
		$this->load->kong("view/board/new");
		$this->load->kong("view/footer");
		$this->_footer();
			
	}
	function viewBoardModify( $id ) {
		
		$this->data->result	= $this->db->result( "select * from `wd_board` where `id`=?", array($id) );
		$this->data->result['options'] = _unserialize($this->data->result['options']);
		
		$this->_header();
		$this->load->kong("view/header");
		$this->load->kong("view/board/modify");
		$this->load->kong("view/footer");
		$this->_footer();
			
			
	}
	function viewBoardCategory( $id ) {

		$this->js("js/board_category");

		$this->data->result	= $this->db->result( "select * from `wd_board` where `id`=?", array($id) );
		$this->data->rows = $this->db->fetch_assoc("select * from `wd_category` where `board_id`=? order by `order` asc limit 0, 256", array($id) );
		
		$this->_header();
		$this->load->kong("view/header");
		$this->load->kong("view/board/category");
		$this->load->kong("view/footer");
		$this->_footer();
		
	}
	function viewBoardGrant( $id ) {

		$this->data->result	= $this->db->result( "select * from `wd_board` where `id`=?", array($id) );
		$this->data->grant	= empty($this->data->result['grant']) ? null : unserialize($this->data->result['grant']);
		$this->data->group	= $this->db->fetch_assoc("select * from `wd_group`");
		
		$this->_header();
		$this->load->kong("view/header");
		$this->load->kong("view/board/grant");
		$this->load->kong("view/footer");
		$this->_footer();
		
	}

	function viewConfigure() {
		$this->_header();
		$this->load->kong("view/header");
		//$this->load->kong("view/new");
		$this->load->kong("view/footer");
		$this->_footer();
	}
}